﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL = class_BL;
using EN = class_Entidad;

namespace WebMenu.Pagina.Mantenimiento
{
    public partial class frmItemsLista : System.Web.UI.Page
    {
        #region "Metodo"
        public void cargaListaGrupo()
        {
            ddlGrupo.Items.Clear();
            List<EN.ITEMS_EN> lista = new List<EN.ITEMS_EN>();
            EN.ITEMS_EN objItem = new EN.ITEMS_EN();
            objItem.ID_ITEMS = 0;
            objItem.DESC_ITEMS = "";
            ddlGrupo.Items.Add(new ListItem("--Todos--", "-1"));
            lista = BL.ITEMS_BL.ListaGrupo(objItem);

            foreach (var item in lista)
            {
                ddlGrupo.Items.Add(new ListItem(Convert.ToString(item.DESC_ITEMS), Convert.ToString(item.ID_ITEMS)));
            }
        }

        public void cargaListaGrupoEdit()
        {
            ddlGrupoE.Items.Clear();
            List<EN.ITEMS_EN> lista = new List<EN.ITEMS_EN>();
            EN.ITEMS_EN objItem = new EN.ITEMS_EN();
            objItem.ID_ITEMS = 0;
            objItem.DESC_ITEMS = "";
            ddlGrupoE.Items.Add(new ListItem("--Seleccionar--", "-2"));
            lista = BL.ITEMS_BL.ListaGrupo(objItem);

            foreach (var item in lista)
            {
                ddlGrupoE.Items.Add(new ListItem(Convert.ToString(item.DESC_ITEMS), Convert.ToString(item.ID_ITEMS)));
            }
        }

        public void cargarLstaItems()
        {
            string strDescr = txtDescripcion.Text;
            List<EN.ITEMS_EN> lista = new List<EN.ITEMS_EN>();
            EN.ITEMS_EN objItem = new EN.ITEMS_EN();
            objItem.ID_ITEMS = 0;
            objItem.ID_PADRE = Convert.ToInt32(ddlGrupo.SelectedValue);
            objItem.DESC_ITEMS = strDescr;
            lista = BL.ITEMS_BL.ListaITEMSGrupo(objItem);
            gvItem.DataSource = lista;
            gvItem.DataBind();
        }
        #endregion

        #region "Evento"
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cargaListaGrupo();
                cargarLstaItems();
            }
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            cargarLstaItems();
        }

        protected void rbtTipo_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rbtTipo.SelectedValue)
            {
                case "G":
                    txtGrupo.Visible = true;
                    txtItems.Visible = false;
                    ddlGrupoE.Visible = false;
                    break;
                case "I":
                    txtItems.Visible = true;
                    ddlGrupoE.Visible = true;
                    txtGrupo.Visible = false;
                    cargaListaGrupoEdit();

                    break;
            }
        }

        protected void btnGrabar_Click(object sender, EventArgs e)
        {
            int valor = 0;
            EN.ITEMS_EN objItem = new EN.ITEMS_EN();
            try
            {
                if (hdIAccion.Value == "N")
                {
                    if (rbtTipo.SelectedValue == "G")
                    {
                        objItem.ID_PADRE = 0;
                        objItem.DESC_ITEMS = txtGrupo.Text;
                    }

                    if (rbtTipo.SelectedValue == "I")
                    {
                        objItem.ID_PADRE = Convert.ToDecimal(ddlGrupoE.SelectedValue);
                        objItem.DESC_ITEMS = txtItems.Text;
                    }

                    valor = BL.ITEMS_BL.RegistroITEMS(objItem);
                }
                if (hdIAccion.Value == "E")
                {
                    objItem.ID_PADRE = Convert.ToDecimal(ddlGrupoE.SelectedValue);
                    objItem.ID_ITEMS = Convert.ToDecimal(hdItemsReg.Value);
                    objItem.DESC_ITEMS = txtItems.Text;
                    objItem.EST_ITEMS = 1;
                    valor = BL.ITEMS_BL.EditarITEMS(objItem);
                }

                if (valor > 0)
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "mensaje", "MensajeAccion('1','');", true);
                else
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "mensaje", "MensajeAccion('2','');", true);

            }
            catch (Exception ex)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Se presentro un error, volver a intentarlo.');", true);
            }
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in rbtTipo.Items)
            {
                item.Selected = false;
            }

            txtGrupo.Visible = false;
            ddlGrupoE.Visible = false;
            txtItems.Visible = false;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Popup", "MostrarPopUp();", true);
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {
            txtItems.Visible = true;
            cargaListaGrupoEdit();
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Popup", "EditarRegistro();", true);
        }

        protected void btnEliminar_Click(object sender, EventArgs e)
        {
            GridView gv = new GridView();
            try
            {
                gv = gvItem;
                foreach (GridViewRow fila in gv.Rows)
                {
                    if (((CheckBox)fila.Cells[0].FindControl("cbSelec")).Checked == true)
                    {
                        int valor = 0;
                        EN.ITEMS_EN objItem = new EN.ITEMS_EN();
                        objItem.ID_PADRE = Convert.ToDecimal(((HiddenField)fila.Cells[0].FindControl("hdIDPadre")).Value);
                        objItem.ID_ITEMS = Convert.ToDecimal(((HiddenField)fila.Cells[0].FindControl("hdID_ITEMS")).Value);
                        objItem.DESC_ITEMS = Convert.ToString(((HiddenField)fila.Cells[0].FindControl("hdDESC_ITEMS")).Value);
                        objItem.EST_ITEMS = (Convert.ToDecimal(((HiddenField)fila.Cells[0].FindControl("hdEst")).Value) == 1) ? 0 : 1;
                        valor = BL.ITEMS_BL.EditarITEMS(objItem);
                    }
                }
                cargarLstaItems();
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Se actualizo el estado de los registros selec.');", true);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Se presentro un error, volver a intentarlo.');", true);
            }

        }
        #endregion
    }
}